#include <QtGui>
#include <QTranslator>
#include <QtCore/QTextCodec>
#include "gv_setuptimedialog.h"
#include "gv_home.h"
#include "gv_setuptimezonedialog.h"
#include "gv_setting.h"

extern GV_Setting *gvsetting;
extern QDialog  *dialog;
GV_SetupTimeDialog::GV_SetupTimeDialog(QWidget *parent)
       : QDialog(parent)
{
    setupUi(this);

    QTranslator translator;
    translator.load(gvsetting->GV_GetLanguage(),".");
    qApp->installTranslator(&translator);
    reloadtext();

    TimeHintsLabel->setText((QDateTime::currentDateTime().toString("hh:mm:ss")));
    DateHintsLabel->setText((QDateTime::currentDateTime().toString("yyyy-MM-dd")));

    TimeFrame->show();
    DisplaymodeFrame->hide();
    timestatus = TIMESTATUSHOUR;
    mydisplaymode = DISPLAYMODE24HOURS;
    myhour=myminute=mysecond = 0;
    TimeValueLabel->setText(QString::number(myhour));

    timer = new QTimer;
    connect(timer,SIGNAL(timeout()),this,SLOT(GV_chTime()));
    connect(BackButton,SIGNAL(clicked()),this,SLOT(cancel()));
    connect(CancelButton,SIGNAL(clicked()),this,SLOT(cancel()));
    connect(AcceptButton,SIGNAL(clicked()),this,SLOT(accept()));

    connect(TimeSelectButton,SIGNAL(clicked()),this,SLOT(timeselect()));
    connect(DisplaymodeSelectButton,SIGNAL(clicked()),this,SLOT(displaymodeselect()));

    connect(HourButton,SIGNAL(clicked()),this,SLOT(setuphour()));
    connect(MinuteButton,SIGNAL(clicked()),this,SLOT(setupminute()));
    connect(SecondButton,SIGNAL(clicked()),this,SLOT(setupsecond()));

    connect(DecreaseButton,SIGNAL(clicked()),this,SLOT(decreasevalue()));
    connect(IncreaseButton,SIGNAL(clicked()),this,SLOT(increasevalue()));

    connect(Hour12Radio,SIGNAL(clicked()),this,SLOT(check12hour()));
    connect(Hour24Radio,SIGNAL(clicked()),this,SLOT(check24hour()));

    timer->start(1000);
}
void GV_SetupTimeDialog::reloadtext()
{
    //NameHintsLabel->setText(tr("Zones"));
}
void GV_SetupTimeDialog::GV_chTime()
{
    TimeHintsLabel->setText((QDateTime::currentDateTime().toString("hh:mm:ss")));
    DateHintsLabel->setText((QDateTime::currentDateTime().toString("yyyy-MM-dd")));
}

void GV_SetupTimeDialog::accept()
{
    QDialog *dialog1 = new GV_SetupTimezoneDialog;
    dialog1->show();
    delete dialog;
    dialog = dialog1;
}

void GV_SetupTimeDialog::cancel()
{
    QDialog *dialog1 = new GV_SetupTimezoneDialog;
    dialog1->show();
    delete dialog;
    dialog = dialog1;
}

void GV_SetupTimeDialog::timeselect()
{
    DisplaymodeFrame->hide();
    TimeFrame->show();
}
void GV_SetupTimeDialog::displaymodeselect()
{
    DisplaymodeFrame->show();
    TimeFrame->hide();
    Hour12Radio->setChecked(true);
}
void GV_SetupTimeDialog:: setuphour()
{
    myhour = 0;
    timestatus = TIMESTATUSHOUR;
    TimeValueLabel->setText(QString::number(myhour));
}
void GV_SetupTimeDialog:: setupminute()
{
    myminute = 0;
    timestatus = TIMESTATUSMINUTE;
    TimeValueLabel->setText(QString::number(myminute));
}
void GV_SetupTimeDialog:: setupsecond()
{
    mysecond = 0;
    timestatus = TIMESTATUSSECOND;
    TimeValueLabel->setText(QString::number(mysecond));
}

void GV_SetupTimeDialog:: decreasevalue()
{
    switch(timestatus){
    case TIMESTATUSHOUR:
        if(myhour==0){
            myhour = 23;
        }else{
            myhour--;
        }
        TimeValueLabel->setText(QString::number(myhour));
        break;
    case TIMESTATUSMINUTE:
        if(myminute==0){
            myminute = 59;
        }else{
            myminute--;
        }
        TimeValueLabel->setText(QString::number(myminute));
        break;
    case TIMESTATUSSECOND:
        if(mysecond==0){
            mysecond = 59;
        }else{
            mysecond--;
        }
        TimeValueLabel->setText(QString::number(mysecond));
        break;
    default:
        break;
    }
}

void GV_SetupTimeDialog:: increasevalue()
{
    switch(timestatus){
    case TIMESTATUSHOUR:
        if(myhour==23){
            myhour = 0;
        }else{
            myhour++;
        }
        TimeValueLabel->setText(QString::number(myhour));
        break;
    case TIMESTATUSMINUTE:
        if(myminute==59){
            myminute = 0;
        }else{
            myminute++;
        }
        TimeValueLabel->setText(QString::number(myminute));
        break;
    case TIMESTATUSSECOND:
        if(mysecond==50){
            mysecond = 0;
        }else{
            mysecond++;
        }
        TimeValueLabel->setText(QString::number(mysecond));
        break;
    default:
        break;
    }
}

void GV_SetupTimeDialog:: check12hour()
{
    mydisplaymode = DISPLAYMODE12HOURS;
}
void GV_SetupTimeDialog:: check24hour()
{
    mydisplaymode = DISPLAYMODE24HOURS;
}
